package com.huitone.smspfm.dao.oracle;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import com.huitone.smspfm.dao.oracle.entity.BlackUse;
import com.huitone.smspfm.dao.oracle.entity.ToBlackUser;

public class BlackuseDao extends BaseDao<BlackUse> {

	@Override
	public List<BlackUse> getAllObj() {
		return jdbcTemplate.query("SELECT * FROM " + tableName + " WHERE STATUS = '1'", new BeanPropertyRowMapper<BlackUse>(BlackUse.class));
	}
	
	public void insert(ToBlackUser toBlackUser){
		String recId =UUID.randomUUID().toString().replace("-","");
		jdbcTemplate.update("INSERT INTO "+tableName+"(rec_id,svr_node,service_code,black_user,status) VALUES(?,?,?,?,?)",
				new Object[]{recId,toBlackUser.getSvrNode(),toBlackUser.getServiceCode(),toBlackUser.getUser(),"1"});
	}
	
	/**
	 * 查询是否已添加黑名单
	 * @param srvNode
	 * @param serviceCode
	 * @param user
	 * @return
	 */
	public boolean isInList(String srvNode, String serviceCode, String user) {
		return jdbcTemplate.queryForList("SELECT * FROM " + tableName + " WHERE svr_node = ? AND service_code = ? AND black_user = ? AND STATUS = '1'", new Object[]{srvNode, serviceCode, user}).size() > 0 ? true : false;
	}
	
	public int[] insert(List<BlackUse> list) {
		
		if (list == null) return null;
		
		String sql = "INSERT INTO " + tableName + "(rec_id, svr_node, service_code, black_user, \"DESC\", status, insert_time, insert_staff_id, last_mod_time, last_mod_staff_id) "
				+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		
		return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setString(1, list.get(i).getRecId());
				ps.setString(2, list.get(i).getSvrNode());
				ps.setString(3, list.get(i).getServiceCode());
				ps.setString(4, list.get(i).getBlackUser());
				ps.setString(5, list.get(i).getDesc());
				ps.setString(6, list.get(i).getStatus());
				Date insertTime = list.get(i).getInsertTime();
				if (insertTime != null) {
					ps.setDate(7, new java.sql.Date(insertTime.getTime()));
				} else {
					ps.setNull(7, java.sql.Types.DATE);
				}
				ps.setString(8, list.get(i).getInsertStaffId());
				Date lastModTime = list.get(i).getLastModTime();
				if (lastModTime != null) {
					ps.setDate(9, new java.sql.Date(lastModTime.getTime()));
				} else {
					ps.setNull(9, java.sql.Types.DATE);
				}
				ps.setString(10, list.get(i).getLastModStaffId());
			}
			
			@Override
			public int getBatchSize() {
				return list.size();
			}
		});
	}
	
	public int insert(BlackUse blackUse) {
		
		int result = 0;
		
		if (blackUse != null) {
			int num[] = insert(Collections.singletonList(blackUse));
			
			if (num != null) {
				result = 1;
			}
		}
		
		return result;
	}
	
	/*@SuppressWarnings("unchecked")
	public int[] insert(List<BlackUse> list) {
		
		int result[] =  null;
		
		if (list != null && !list.isEmpty()) {
			
			Map<String,Object>[] maps = new Map[list.size()];
			
			for (int i = 0 ; i < list.size() ; i ++) {
				
				BlackUse blackUse = list.get(i);
				
				Map<String,Object> map = new HashMap<>();
				map.put("rec_id", blackUse.getRecId());
				
				String svrNode = blackUse.getSvrNode();
				if (svrNode != null) {
					map.put("svr_node", svrNode);
				}
				
				String serviceCode = blackUse.getServiceCode();
				if (serviceCode != null) {
					map.put("service_code", serviceCode);
				}
				
				String blackUser = blackUse.getBlackUser();
				if(blackUser != null) {
					map.put("black_user", blackUser);
				} 
				
				String desc = blackUse.getDesc();
				if (desc != null) {
					map.put("desc", desc);
				}
				
				String status = blackUse.getStatus();
				if (status != null) {
					map.put("status", status);
				}
				
				Date insertTime = blackUse.getInsertTime();
				if (insertTime != null) {
					map.put("insert_time", insertTime);
				}
				
				String insertStaffId = blackUse.getInsertStaffId();
				if (insertStaffId != null) {
					map.put("insert_staff_id", insertStaffId);
				}
				
				Date lastModTime = blackUse.getLastModTime();
				if (lastModTime != null) {
					map.put("last_mod_time", lastModTime);
				}
				
				String lastModStaffId = blackUse.getLastModStaffId();
				if (lastModStaffId != null) {
					map.put("last_mod_staff_id", lastModStaffId);
				}
				
				maps[i] = map;
			}
			result = simpleJdbcInsert.includeSynonymsForTableColumnMetaData().executeBatch(maps);
		}
		
		return result;
	}
	
	public int insert(BlackUse blackUse) {
		
		int result = 0;
		
		if (blackUse != null) {
			int num[] = insert(Collections.singletonList(blackUse));
			
			if (num != null) {
				result = 1;
			}
		}
		
		return result;
	}*/

}
